<?php

use think\facade\View;
use think\facade\Route;

//404
Route::miss(function () {
    return View::fetch('../app/providers/errortpl/404');
});
	
//web
Route::get('/', function () {
	return redirect('/admin');
});

// 获取views css和js
Route::pattern(['path' => '.*\.\w+$'])->get('/admins/views', function () {
	$requestPath =  request()->pathinfo();
	$suffix = substr(strrchr($requestPath, '.'), 1);
	if ($suffix == 'css' || $suffix == 'js') {
		$path = str_replace('admins/', '', $requestPath);
		if (!file_exists($path)) {
			$path = str_replace('admins/', '../', $requestPath);
		}
		return response(file_get_contents($path), 200, ['Content-type' => 'text/' . $suffix]);
	}
});

// 获取文件
Route::pattern(['path' => '.*\.\w+$'])->get('/:path', function (string $path) {
	$suffix = substr(strrchr($path, '.'), 1);
	if ($suffix == 'css' || $suffix == 'js') {
		$filename = public_path() .'admins/'. $path;
		return response(file_get_contents($filename), 200, ['Content-type' => 'text/' . $suffix]);
	}else{
		if( file_exists(public_path() . $path) ){
			return new \think\File(public_path() . $path);
		}else{
			return response(View::fetch('../app/providers/errortpl/404'),404);
		}
	}
});

